﻿using Cms.Application.Admin.Views;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class EditUserPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly IEditUserView _view;
        
        // Code-behind constructor
        public EditUserPresenter(IEditUserView view)
            : this(view, new RepositoryFactory())
        {
        }

        // Unit test constructor
        public EditUserPresenter(IEditUserView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            _view.HeadingText = GetText(_view, _view.EditedUserID > 0 ? "EditHeading" : "NewHeading");
            if (_view.EditedUserID == 0)
            {
                _view.RedirectToBadRequestParameterPage();
                return;
            }
            if ((_view.EditedUserID > 0 && !_administrator.HasPermission.EditUser) || (_view.EditedUserID == -1 && !_administrator.HasPermission.AddUser))
            {
                _view.RedirectToPermissionDeniedPage();
                return;
            }
        }

        public void Initialize()
        {
            if (_view.EditedUserID > 0)
            {
                RefreshProperties();
            }
            else
            {
                _view.SetHeading(string.Empty);
            }
        }

        public void RefreshProperties()
        {
            if (_view.EditedUserID < 0)
            {
                return;
            }
            User user = UserRepository.GetUser(_view.EditedUserID);
            _view.Username = user.Username;
            _view.Password = user.Password;
            _view.Firstname = user.Firstname;
            _view.Lastname = user.Lastname;
            _view.Email = user.Email;
            _view.Phone = user.Phone;
            _view.SetHeading(user.Fullname);
            _view.SetMemberships(user.Memberships);
        }

        public void Save()
        {
            User user = new User
                            {
                                Username = _view.Username,
                                Password = _view.Password,
                                Email = _view.Email,
                                Firstname = _view.Firstname,
                                Lastname = _view.Lastname,
                                Phone = _view.Phone
                            };
            if (_view.EditedUserID > 0)
            {
                user.ID = _view.EditedUserID;
            }
            UserRepository.SaveUser(user);
            _view.SetEditedUserID(user.ID);
        }

        public void DeleteMembership(int id)
        {
            User user = UserRepository.GetUser(_view.EditedUserID);
            Membership membership = user.GetMembership(id);
            membership.UserGroup = null;
            membership.User = null;
            user.Memberships.Remove(membership);
            UserRepository.SaveUser(user);
        }
    }
}
